<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

// output object
function debug($var,$flag = true){
	echo "<pre>"; print_r($var); echo "</pre>";
	if($flag) die("End Debug");
}

//highlight
function highlightWords($text, array $words){
  $words = array_map(preg_quote, $words);
  return preg_replace('/(' . implode('|', $words). ')/is', '<span class="higlight">$1</span>', $text);
}

// return Money Format
function moneyformat($number){
	if($number){
		return number_format($number,0, ',', '.')." $";	
	}else{
		return "0 $";	
	}
}

// convert second to word
function secondsToWords($seconds,$type=0)
{
    /*** return value ***/
    $ret = "";

    /*** get the hours ***/
    $hours = intval(intval($seconds) / 3600);
    if($hours != 0)
    {
        $ret .= str_pad($hours,2,"0",STR_PAD_LEFT);
		if($type==1)
			$ret .=":";
		else $ret .=" Giờ ";
    }else{
		if($type==1)
			$ret .="00:";	
	}
    /*** get the minutes ***/
    $minutes = bcmod((intval($seconds) / 60),60);
    if($hours != 0 || $minutes != 0)
    {
        $ret .= str_pad($minutes,2,"0",STR_PAD_LEFT);
		if($type==1) $ret .=":";
		else $ret .=" Phút ";
    }else{
		if($type==1)
			$ret .="00:";	
	}
  
    /*** get the seconds ***/
    $seconds = bcmod(intval($seconds),60);
	if( $seconds !=0){
    	$ret .= str_pad($seconds,2,"0",STR_PAD_LEFT);
		if($type==1) 
			$ret .=""; 
		else 
			$ret .= " Giây";
	}else{
		if($type==1)
			$ret .="00";	
	}
    return $ret;
}

//do encrypt data
function sys_encrypt($text)
    {
        return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
    }

//do decrypt data
function sys_decrypt($text)
    {
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
    }
	
/**
 * Return the timestamp of the first day (Monday) of a given week in a given year
 * 
 * @param integer week number
 * @param integer Year
 */
function first_day_of_week($week_number, $year) {
	$offset = date('w', mktime(0, 0, 0, 1, 1, $year));
	
	$offset = ($offset < 5) ? (1 - $offset) : (8 - $offset);
	$monday = mktime(0, 0, 0, 1, 1 + $offset, $year);
	$date = strtotime('+' . ($week_number - 1) . ' weeks', $monday);
	
	return $date;
}

/**
 * Return the timestamp of the last day (Sunday) of a given week in a given year
 * 
 * @param integer week number
 * @param integer Year
 */
function last_day_of_week($week_number, $year) {
	$first_day = first_day_of_week($week_number, $year);
	
	return strtotime('+6 days', $first_day);
}

/**
 * Return the number of weeks in a given year
 * 
 * @param integer Year
 */
function num_of_weeks($year) {
	return max(date('W', mktime(0, 0, 0, 12, 25, $year)), date('W', mktime(0, 0, 0, 12, 31, $year)));
}

/**
 * Generate random float number
 */
function frand($min, $max) {
	$range = $max - $min;
	return $min + $range * mt_rand(0, 32767)/32767;
}


/**
 * Truncate a string to a certain length if necessary,
 * optionally splitting in the middle of a word, and
 * appending the $etc string or inserting $etc into the middle.
 * @param string
 * @param integer
 * @param string
 * @param boolean
 * @param boolean
 * @return string
 */
function truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false){
    if ($length == 0)
        return '';

    if (strlen($string) > $length) {
        $length -= min($length, strlen($etc));
        if (!$break_words && !$middle) {
            $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
        }
        if(!$middle) {
            return substr($string, 0, $length) . $etc;
        } else {
            return substr($string, 0, $length/2) . $etc . substr($string, -$length/2);
        }
    } else {
        return $string;
    }
}

function js2PhpTime($jsdate){
  if(preg_match('@(\d+)/(\d+)/(\d+)\s+(\d+):(\d+)@', $jsdate, $matches)==1){
    $ret = mktime($matches[4], $matches[5], 0, $matches[1], $matches[2], $matches[3]);
    //echo $matches[4] ."-". $matches[5] ."-". 0  ."-". $matches[1] ."-". $matches[2] ."-". $matches[3];
  }else if(preg_match('@(\d+)/(\d+)/(\d+)@', $jsdate, $matches)==1){
    $ret = mktime(0, 0, 0, $matches[1], $matches[2], $matches[3]);
    //echo 0 ."-". 0 ."-". 0 ."-". $matches[1] ."-". $matches[2] ."-". $matches[3];
  }
  return $ret;
}

function php2JsTime($phpDate){
    //echo $phpDate;
    //return "/Date(" . $phpDate*1000 . ")/";
    return date("m/d/Y H:i", $phpDate);
}

function php2MySqlTime($phpDate){
    return date("Y-m-d H:i:s", $phpDate);
}

function mySql2PhpTime($sqlDate){
    $arr = date_parse($sqlDate);
    return mktime($arr["hour"],$arr["minute"],$arr["second"],$arr["month"],$arr["day"],$arr["year"]);

}